package com.centaline.androidsalesblog.widgets.dropdown;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.businissobject.DropBo;

import java.util.ArrayList;

/**
 * 三列
 * Created by guilin on 16/1/18.
 */
public class TrebleDrop extends AbsDrop {

    private LinearLayout ll_parent;
    private ListView lv_left, lv_mid, lv_right;
    private DropTextAdapter leftAdapter, midAdapter, rightAdapter;
    private ArrayList<DropBo> list = new ArrayList<>();

    public TrebleDrop(View anchor, Context context) {
        super(anchor, context);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_drop_treble, null);
        ll_parent = (LinearLayout) view.findViewById(R.id.ll_parent);
        ll_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        lv_left = (ListView) view.findViewById(R.id.lv_left);
        lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                leftAdapter.select(position);
                midAdapter.setSelectedPos(0);
                midAdapter.setArrayList(list.get(position).getChildrenList());
                lv_mid.smoothScrollToPosition(0);
                rightAdapter.setArrayList(new ArrayList<DropBo>());
                lv_right.setVisibility(View.GONE);
            }
        });
        lv_mid = (ListView) view.findViewById(R.id.lv_mid);
        lv_mid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                midAdapter.select(position);
                final ArrayList<DropBo> dropBos = midAdapter.getArrayList().get(position).getChildrenList();
                if (dropBos == null) {
                    rightAdapter.setArrayList(new ArrayList<DropBo>());
                    lv_right.setVisibility(View.GONE);
                    final DropBo dropBo = midAdapter.getArrayList().get(position);
                    dropComplete(false, dropBo.getType(), dropBo);
                } else {
                    rightAdapter.setSelectedPos(11 == midAdapter.getArrayList().get(position).getType() ? -1 : 0);
                    rightAdapter.setArrayList(dropBos);
                    lv_right.setVisibility(View.VISIBLE);
                    lv_right.smoothScrollToPosition(0);
                }
            }
        });
        lv_right = (ListView) view.findViewById(R.id.lv_right);
        lv_right.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                rightAdapter.select(position);
                dropComplete(false, rightAdapter.getArrayList().get(position).getType(),
                        midAdapter.getArrayList().get(midAdapter.getSelectedPos()),
                        rightAdapter.getArrayList().get(position));
            }
        });
        leftAdapter = new DropTextAdapter(list);
        midAdapter = new DropTextAdapter();
        rightAdapter = new DropTextAdapter();
        lv_left.setAdapter(leftAdapter);
        lv_mid.setAdapter(midAdapter);
        lv_right.setAdapter(rightAdapter);
        initPopWindow(view);
    }

    @Override
    public void init(ArrayList<DropBo> arrayList) {
        list.clear();
        list.addAll(arrayList);
        leftAdapter.notifyDataSetChanged();
        midAdapter.setArrayList(list.get(0).getChildrenList());
    }


    @Override
    public void onDismiss() {

    }
}
